/**
 * Created by liujing on 17/01/09.
 */
let validate = {
  Anything: {
    rule: /.*/,
    message: 'validate.anything'
  },
  ImgNew: {
    rule: /^\S*\.(?:png|jpg|gif|PNG|JPG|GIF|jpeg|bmp|BMP|JPEG)$/,
    message: 'validate.img'
  },
  Img: {
    rule: /^\S*\.(?:png|jpg|bmp|gif|tiff|PNG|JPG|BMP|GIF|TIFF|jpeg|JPEG)$/,
    message: 'validate.img'
  },
  Email: {
    rule: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
    message: 'validate.email'
  },
  Phone: {
    rule: /^[0-9]\d{2,16}$/,
    message: 'validate.phone'
  },
  Phone2: {
    rule: /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/,
    message: 'validate.phone2'
  },
  Phone3: {
    rule: /(^(((\\+\\d{2}-)?0\\d{2,3}-\\d{7,8})|((\\+\\d{2}-)?(\\d{2,3}-)?([1][3,4,5,7,8][0-9]\\d{8})))$)/,
    message: 'validate.phone2'
  },
  AllPhone: {
    rule: /^([0-9]|-){2,16}$/,
    message: 'validate.allPhone'
  },
  Mobile: {
    // rule: /^0?(13\d{9}|15\d{9}|18\d{9}|14\d{9})$/,
    rule: /^1(3|4|5|6|7|8|9)\d{9}$/,
    message: 'validate.mobile'
  },
  Url: {
    rule: /^((http|ftp|https):\/\/)(([a-zA-Z0-9._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(\/[a-zA-Z0-9&%_./-~-]*)?/,
    message: 'validate.url'
  },
  TEXT: {
    rule: /^\w+$/,
    message: 'validate.text'
  },
  Number: {
    rule: /^\d+$/,
    message: 'validate.number'
  },
  Number2: {
    rule: /^\d+(\.\d{1,2})?$/,
    message: 'validate.number2'
  },
  Number3: {
    rule: /^[0-9]\d*$/, // 正整数
    message: 'validate.number3'
  },
  Number5: {
    rule: /^[1-9]\d*$/, // 正整数
    message: 'validate.number3'
  },
  QQ: {
    rule: /^[1-9]\d{4,8}$/,
    message: 'validate.QQ'
  },
  Number4: {
    rule: /^[1-9]\d{0,2}$/,
    message: 'validate.number4'
  },
  Integer: {
    rule: /^[-+]?\d+$/,
    message: 'validate.integer'
  },
  Double: {
    rule: /^[-+]?\d+(\.\d+)?$/,
    message: 'validate.double'
  },
  Character: {
    rule: /^[A-Za-z0-9_]+$/,
    message: 'validate.character'
  },
  Character2: {
    rule: /^[A-Za-z0-9._]+$/,
    message: 'validate.character2'
  },
  IP: {
    rule: /^(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][0-9])|([0-9]))\.(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][0-9])|([0-9]))\.(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][0-9])|([0-9]))\.(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][0-9])|([0-9]))$/,
    message: 'validate.IP'
  },
  YYYYMMDDHHDDSS: {
    rule: /^[12][0-9]{3}-([0][1-9]|[1][012])-([012][0-9]|[3][01]) ([01][0-9]|[2][0-3]):[0-5][0-9]:[0-5][0-9]$/,
    message: 'validate.time_s'
  },
  YYYYMMDD: {
    rule: /^[12][0-9]{3}-([0][0-9]|[1][012])-([012][0-9]|[3][01])$/,
    message: 'validate.time_d'
  },
  Chinese: {
    rule: /^[\u0391-\uFFE5]+$/,
    message: 'validate.Chinese'
  },
  NotChinese: {
    rule: /^[^\u0391-\uFFE5]+$/,
    message: 'validate.notChinese'
  },
  hasChinese: {
    rule: /[\u0391-\uFFE5]+/,
    message: 'validate.hasChinese'
  },
  UnSafe: {
    rule: /^(([A-Z]*|[a-z]*|\d*|[-_~!@#$%^&*.()[\]{}<>?\\/'"]*)|.{0,5})$|\s/,
    message: 'validate.unSafe'
  },
  Age: {
    rule: /^[1-9]\d{0,2}$/,
    message: 'validate.age'
  },
  HtmlEnd: {
    rule: /<("[^"]*"|'[^']*'|[^'">])*>$/, // 以html标签结尾
    message: 'validate.html'
  },
  specialCharacter: {
    rule: /[\\:*<>"|?/^+&]/,
    message: 'validate.specialCharacter'
  }
}
export function checkform (value, type, paneNode) {
  if (value && type) {
    let rule = validate[type].rule
    // let result = !!value.match(rule)
    let result = !!rule.test(value)
    if (!result) {
      if (paneNode) {
        formRollToFirstError(paneNode) // 表单校验有问题时，滚动到第一个el-error的位置
      }
      return validate[type].message
    } else {
      return ''
    }
  }
}
export function formRollToFirstError (paneNode) { // 表单校验有问题时，滚动到第一个el-error的位置
  setTimeout(() => { // 延时是为了解决偶发异步取不到is-error节点
    let pane = paneNode.getElementsByClassName('is-error')[0]
    if (pane) {
      let textareaNode = pane.getElementsByClassName('el-textarea__inner') // 多行文本
      let inputNode = pane.getElementsByClassName('el-input__inner') // 单行文本，日期，日期时间，下拉框，数字等
      let checkboxNode = pane.getElementsByClassName('el-checkbox__original') // 复选框字段
      let radioNode = pane.getElementsByClassName('el-radio__original') // 单选框字段
      let fujianNode = pane.getElementsByClassName('el-button el-button--text icon-fujian') // 上传附件
      if (textareaNode && textareaNode[0]) {
        textareaNode[0].focus()
      } else if (inputNode && inputNode[0]) {
        inputNode[0].focus()
      } else if (checkboxNode && checkboxNode[0]) {
        checkboxNode[0].focus()
      } else if (radioNode && radioNode[0]) {
        radioNode[0].focus()
      } else if (fujianNode && fujianNode[0]) {
        fujianNode[0].focus()
      }
    }
  }, 500)
}
